<?php
/**
 * Created by PhpStorm.
 * User: 陶然
 * Date: 2019/4/9
 * Time: 21:40
 * SOURCE: 中国大陆工时规管现况（标准工时）：
 *              一天工作时间为8小时，平均每周工时不超过40小时;
 *              加班上限为一天3小时及一个月36小时，逾时工作薪金不低于平日工资的150％。
 *              而一周最高工时则为48小时。平均每月计薪天数为21.75天。
 * TO: 我是陶然,健康撸代码,拒绝通宵。
 * CLASS: < 操作模块 >
 */
namespace App\HttpController\Taoran;

use App\HttpController\V1\Auth;
use App\HttpController\Taoran\Functions\Publics;
class Operation extends Auth
{
    static public $studyType=[8=>1,9=>2,11=>3,3=>4,2=>5];
    /**
     * @return bool
     * 获取手机号
     */
    function getmobile()
    {
        $mobile=$this->db->where('id',$this->user->id)->getValue('users','mobile');
        if (!$mobile){
            return $this->writeJson(201,'','暂未绑定手机号');
        }
        $phone=substr($mobile, 0, 3).'****'.substr($mobile, 7);
        return $this->writeJson(200,$phone);
    }

    /**
     * @return bool
     * 收藏
     */
    function collection()
    {
        $ids=$this->input('ids','post');//
        $clas_id=$this->db->where('id',$ids)->getOne('study','class_id,favorite');

        if (!empty($this->db->where('user_id',$this->user->id)->where('study_id',$ids)->getOne('users_collection'))){
            $this->db->where('user_id',$this->user->id)->where('study_id',$ids)->delete('users_collection',1);
            $this->db->where('id',$ids)->setDec('study','favorite',1);
            return $this->writeJson(200,$clas_id['favorite']-1,'取消收藏成功');
        }

        if (empty(self::$studyType[$clas_id['class_id']])){
            do{
                $clas_id['class_id']=$this->db->where('id',$clas_id['class_id'])->getValue('study_class','pid');
            }while(empty(self::$studyType[$clas_id['class_id']]));
        }
        $data=[
            'class_id'=>self::$studyType[$clas_id['class_id']],
            'user_id'=>$this->user->id,
            'study_id'=>$ids,
            'create_time'=>date('YmdHis')
        ];
        $this->db->where('id',$ids)->setInc('study','favorite',1);

        $sql=$this->db->insert('users_collection',$data);
        if (!$sql){
            return $this->writeJson(205,'','收藏失败');
        }
        return $this->writeJson(200,$clas_id['favorite']+1,'收藏成功');
    }

    /**
     * 评论/回复评论
     */
    function comment(){
        //评论
        $param=$this->input('id,study_id,to_user_id,content');
        $data=[];
        if(!$param['id']){
            $data['study_id']=$param['study_id'];
            $data['user_id']=$this->user->id;
            $data['content']=$param['content'];
            $data['create_time']=date('Y-m-d H:i:s');
        }else{
            $data['study_id']=$param['study_id'];
            $data['pid']=$param['id'];
            $data['user_id']=$this->user->id;
            $data['to_user_id']=$param['to_user_id'];
            $data['content']=$param['content'];
            $data['create_time']=date('Y-m-d H:i:s');
        }
        try{
            $this->db->insert('study_comment',$data);
            $this->db->where('id',$param['study_id'])->setInc('study','comm_num');
            return $this->writeJson(200,$param['study_id'],'成功');
        }catch (\Exception $e){
            return $this->writeJson(-201,null,$e->getMessage());
        }
    }

    /**
     * 点赞
     * @return bool
     */
    function like(){
        $id=$this->input('id');
        $like=$this->db->where('id',$id)->getValue('study','`like`');

        if($this->db->where('user_id',$this->user->id)->where('study_id',$id)->has('study_like')){
            $this->db->where('user_id',$this->user->id)->where('study_id',$id)->delete('study_like',1);
            $this->db->where('id',$id)->update('study',['like'=>$like-1]);
            return $this->writeJson(200,$like-1,'取消点赞');
        }

        $data=[
            'user_id'=>$this->user->id,
            'study_id'=>$id,
            'create_time'=>date('YmdHis')
        ];

        $this->db->where('id',$id)->update('study',['like'=>$like+1]);
        $sql=$this->db->insert('study_like',$data);
        if (!$sql){
            return $this->writeJson(-201,null,'点赞失败');
        }
        return $this->writeJson(200,$like+1,'点赞成功');
    }

    /**
     * 关注/取消关注
     */
    function follow(){
        $to_user_id=$this->input('to_user_id');

        if($this->db->where('user_id',$this->user->id)->where('to_user_id',$to_user_id)->has('users_follow')){
            $this->db->where('user_id',$this->user->id)->where('to_user_id',$to_user_id)->delete('users_follow',1);
            return $this->writeJson(200,null,'取消关注');
        }
        $data=[
            'user_id'=>$this->user->id,
            'to_user_id'=>$to_user_id,
            'create_time'=>date('YmdHis')
        ];
        $sql=$this->db->insert('users_follow',$data);
        if (!$sql){
            return $this->writeJson(-201,null,'关注失败');
        }
        return $this->writeJson(200,null,'关注成功');
    }




}